package com.think.admin.web.controller;

import com.think.admin.annotations.MenuAnnotation;
import com.think.admin.annotations.ResourceAnnotation;
import com.think.admin.model.dto.user.SysUserDTO;
import com.think.admin.model.param.user.SetUserRoleParam;
import com.think.admin.model.param.user.SysUserParam;
import com.think.admin.model.query.SysUserQuery;
import com.think.admin.web.service.ISysUserService;
import com.think.admin.web.service.IUserRoleService;
import com.think.common.param.UpdateBatchParam;
import com.think.common.utils.ResultUtil;
import com.think.common.utils.SysUtils;
import com.think.common.validation.CreateOrUpdate;
import com.think.common.validation.Update;
import com.think.common.web.controller.SuperController;
import com.think.dao.entity.SysUser;
import com.think.dao.entity.UserRole;
import com.think.dao.enums.ResourcePermissionTypeEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 管理员用户
 * </p>
 *
 * @author: xwj
 * @data: 2019-04-10
 */
@RestController
@RequestMapping(value = "admin/sysUser")
@RequiredArgsConstructor
@MenuAnnotation(name = "系统用户", sort = 1, permissionType = ResourcePermissionTypeEnum.PERMISSION)
public class SysUserController extends SuperController {
    private final ISysUserService sysUserService;
    private final IUserRoleService userRoleService;

    @GetMapping
    @ResourceAnnotation(name = "查看", sort = 1, permissionType = ResourcePermissionTypeEnum.PERMISSION)
    public HttpEntity page(SysUserQuery sysUserQuery) {
        return sysUserService.pageSysUser(getPage(), sysUserQuery);
    }

    @PostMapping
    @ResourceAnnotation(name = "新增修改", sort = 2, permissionType = ResourcePermissionTypeEnum.PERMISSION)
    public HttpEntity createOrUpdate(@RequestBody @Validated(CreateOrUpdate.class) SysUserParam sysUserParam) {
        return sysUserService.createOrUpdate(sysUserParam);
    }

    @PutMapping("/batch-delete")
    @ResourceAnnotation(name = "删除", sort = 3, permissionType = ResourcePermissionTypeEnum.PERMISSION)
    public HttpEntity delete(@RequestBody List<String> idList) {
        return sysUserService.delete(idList);
    }

    @PutMapping("/batch-updateState")
    @ResourceAnnotation(name = "启用禁用", sort = 4, permissionType = ResourcePermissionTypeEnum.PERMISSION)
    public HttpEntity updateState(@RequestBody @Validated(UpdateBatchParam.Update.class) UpdateBatchParam updateBatchParam) {
        return sysUserService.updateState(updateBatchParam);
    }

    @PutMapping("/batch-resetPwd")
    @ResourceAnnotation(name = "重置密码", sort = 5, permissionType = ResourcePermissionTypeEnum.PERMISSION)
    public HttpEntity resetPwd(@RequestBody List<String> idList) {
        return sysUserService.resetPwd(idList);
    }

    @PutMapping("/setUserRole")
    @ResourceAnnotation(name = "用户角色", sort = 6, permissionType = ResourcePermissionTypeEnum.PERMISSION)
    public HttpEntity setUserRole(@RequestBody @Validated(Update.class) SetUserRoleParam setUserRoleParam) {
        return sysUserService.setUserRole(setUserRoleParam);
    }

    @GetMapping("/{id}")
    @ResourceAnnotation(name = "用户详情", sort = 7, permissionType = ResourcePermissionTypeEnum.LOGIN)
    public HttpEntity get(@PathVariable("id") String id) {
        // 根据id查询
        SysUser sysUser = sysUserService.getById(id);

        // 非空判断
        if (SysUtils.isEmpty(sysUser)) {
            return ResultUtil.error(HttpStatus.GONE, "资源不存在");
        }

        // 类型转换
        SysUserDTO sysUserDTO = sysUser.convert(SysUserDTO.class);

        // 查询用户角色
        List<String> roleIdList = userRoleService.lambdaQuery().select(UserRole::getRoleId)
                .eq(UserRole::getUserId, sysUser.getUserId())
                .list().stream().filter(SysUtils::isNotEmpty).map(UserRole::getRoleId).collect(Collectors.toList());
        sysUserDTO.setRoleIdList(roleIdList);

        //返回
        return ResultUtil.success(sysUserDTO);
    }

    @PostMapping("/selectList")
    @ResourceAnnotation(name = "销售人员查看", sort = 8, permissionType = ResourcePermissionTypeEnum.LOGIN)
    public HttpEntity selectList(@RequestBody SysUserQuery sysUserQuery) {
        return sysUserService.pageSysUserSelectList(getPage(),sysUserQuery);
    }
}
